package org.jeecg.modules.zcgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.zcgl.entity.PayablesDetails;
import org.jeecg.modules.zcgl.entity.PayablesInfo;
import org.jeecg.modules.zcgl.enums.PaymentStatusEnum;
import org.jeecg.modules.zcgl.mapper.PayablesDetailsMapper;
import org.jeecg.modules.zcgl.mapper.PayablesInfoMapper;
import org.jeecg.modules.zcgl.service.IPayablesInfoService;
import org.jeecg.modules.zcgl.vo.statistical.PersonalOnlinePlatformPaymentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 应付款项
 * @Author: jeecg-boot
 * @Date:   2020-06-30
 * @Version: V1.0
 */
@Service
public class PayablesInfoServiceImpl extends ServiceImpl<PayablesInfoMapper, PayablesInfo> implements IPayablesInfoService {

	@Autowired
	private PayablesInfoMapper payablesInfoMapper;
	@Autowired
	private PayablesDetailsMapper payablesDetailsMapper;
	
	@Override
	@Transactional
	public void saveMain(PayablesInfo payablesInfo, List<PayablesDetails> payablesDetailsList) {
		payablesInfo.setPayStatus(PaymentStatusEnum.UNPAID.getStatus());
		BigDecimal totalMoney = BigDecimal.ZERO;
		payablesInfo.setTotalMoney(totalMoney);
		payablesInfoMapper.insert(payablesInfo);

		//子表数据插入
		this.saveDetails(payablesDetailsList, payablesInfo);

		//修改总金额
		payablesInfoMapper.updateById(payablesInfo);
	}

	@Override
	@Transactional
	public void updateMain(PayablesInfo payablesInfo,List<PayablesDetails> payablesDetailsList) {
		if (payablesInfo.getPayStatus() == null){
			payablesInfo.setPayStatus(PaymentStatusEnum.UNPAID.getStatus());
		}
		
		//1.先删除子表数据
		payablesDetailsMapper.deletePhysicalByMainId(payablesInfo.getId());

		//2.子表数据重新插入
		this.saveDetails(payablesDetailsList, payablesInfo);

		payablesInfoMapper.updateById(payablesInfo);
	}

	/**
	 * 保存详情，计算出总金额
	 * @param payablesDetailsList
	 * @param payablesInfo
	 */
	private void saveDetails(List<PayablesDetails> payablesDetailsList, PayablesInfo payablesInfo){

		BigDecimal totalMoney = BigDecimal.ZERO;
		if(payablesDetailsList==null || payablesDetailsList.size() == 0) {
			throw new JeecgBootException("请输入应付款项详情！");
		}

		for(PayablesDetails entity:payablesDetailsList) {

			if (entity.getAmountMoney() == null || entity.getAmountMoney().compareTo(BigDecimal.ZERO) <= 0){
				throw new JeecgBootException("请输入正确的应付款金额！");
			}

			//外键设置
			entity.setAssetId(payablesInfo.getAssetId());
			entity.setAssetSubIds(payablesInfo.getAssetSubIds());
			entity.setLeaseContractId(payablesInfo.getLeaseContractId());
			entity.setCustomerId(payablesInfo.getCustomerId());
			entity.setPayablesInfoId(payablesInfo.getId());
			payablesDetailsMapper.insert(entity);

			totalMoney = totalMoney.add(entity.getAmountMoney());
		}

		payablesInfo.setTotalMoney(totalMoney);
	}

	@Override
	@Transactional
	public void delMain(String id) {
		payablesDetailsMapper.deleteByMainId(id);
		payablesInfoMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			payablesDetailsMapper.deleteByMainId(id.toString());
			payablesInfoMapper.deleteById(id);
		}
	}

	@Override
	public IPage<PersonalOnlinePlatformPaymentVo> queryPersonalOnlinePlatformPaymentPageList(Page<PersonalOnlinePlatformPaymentVo> page, QueryWrapper<PersonalOnlinePlatformPaymentVo> queryWrapper) {
		return baseMapper.queryPersonalOnlinePlatformPaymentPageList(page,queryWrapper);
	}

	@Override
	public List<PersonalOnlinePlatformPaymentVo> queryPersonalOnlinePlatformPaymentList(QueryWrapper<PersonalOnlinePlatformPaymentVo> queryWrapper) {
		return baseMapper.queryPersonalOnlinePlatformPaymentPageList(queryWrapper);
	}

}
